package fileConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map.Entry;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class writeExcel {

	public static void main(String[] args) {
		HashMap<String, sheetData> excelData = new readExcel().readExcel("resource/webHomePageTest.xlsx");
		for (Entry<String, sheetData> entry : excelData.entrySet()) {
			System.out.println("sheetName=" + entry.getKey() + "  data= \r\n");
			for (HashMap<String, Object> data : entry.getValue().getSheetData()) {
				for (Entry<String, Object> rowdata : data.entrySet()) {
					System.out.print(rowdata.getKey() + "=" + rowdata.getValue() + "   ");
				}
				System.out.println();
			}
		}

		// new writeExcel().createEexelFile("resource/staticTestFile.xlsx",excelData);
		new writeExcel().addDataEexelFile("resource/staticTestFile.xlsx", excelData);
	}

	/**
	 * 创建excel文件并写入数据从第0行开始
	 * 
	 * @param filePath
	 * @param data
	 * @throws IOException
	 */
	public void createEexelFile(String filePath, HashMap<String, sheetData> map) {
		File file = new File(filePath);
		String excelFileName = new File(filePath).getAbsolutePath();
		Workbook workbook = null;
		if (file.exists()) {
			try {
				InputStream inputStream = new FileInputStream(new File(filePath));
				String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length());
				workbook = new readExcel().getWorkbook(inputStream, fileType);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else {
			workbook = new SXSSFWorkbook();
		}
		for (Entry<String, sheetData> entry : map.entrySet()) {
			System.out.println(entry.getValue().getSheetTitle());
			Sheet sheet = workbook.createSheet(entry.getValue().getSheetTitle());
			int cellNum = 0, rowNum = 0;
			Row row = sheet.createRow(rowNum++);
			Cell cell;
			for (String rowData : entry.getValue().getShellHeader()) {
				cell = row.createCell(cellNum++);
				cell.setCellValue(null == rowData.toString() ? "" : rowData.toString());
			}
			convertDataToRow(sheet, entry.getValue(), 1);
		}
		writerFile(workbook, excelFileName);
	}

	/**
	 * excel文件并追加数据
	 * 
	 * @param filePath
	 * @param data
	 * @throws IOException
	 */
	public void addDataEexelFile(String filePath, HashMap<String, sheetData> map) {
		String excelFileName = new File(filePath).getAbsolutePath();
		Workbook workbook = null;
		try {
			InputStream inputStream = new FileInputStream(new File(filePath));
			String fileType = filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length());
			workbook = new readExcel().getWorkbook(inputStream, fileType);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		for (Entry<String, sheetData> entry : map.entrySet()) {
			Sheet sheet = workbook.getSheet(entry.getValue().getSheetTitle());
			convertDataToRow(sheet, entry.getValue(), sheet.getPhysicalNumberOfRows());
		}
		writerFile(workbook, excelFileName);
	}

	public void writerFile(Workbook workbook, String filePath) {
		FileOutputStream fileOut = null;
		try {
			fileOut = new FileOutputStream(filePath);
			workbook.write(fileOut);
			fileOut.flush();
		} catch (Exception e) {
			System.out.println("输出Excel时发生错误，错误原因：" + e.getMessage());
		} finally {
			try {
				if (null != fileOut) {
					workbook.close();
				}
				if (null != workbook) {
					fileOut.close();
				}
			} catch (IOException e) {
				System.out.println("关闭输出流时发生错误，错误原因：" + e.getMessage());
			}
		}
	}

	/**
	 * 按行存储数据
	 * 
	 * @param data 源数据
	 * @param row  行对象
	 * @return
	 */
	public void convertDataToRow(Sheet sheet, sheetData data, int rowNum) {
		int cellNum = 0;
		Cell cell;
		for (HashMap<String, Object> entry : data.getSheetData()) {
			Row row = sheet.createRow(rowNum++);
			for (String rowName : data.getShellHeader()) {
				cell = row.createCell(cellNum++);
				cell.setCellValue(null == entry.get(rowName) ? "" : entry.get(rowName).toString());
			}
			cellNum = 0;
		}
	}

}
